/**
 * 用于在action中暴露出dispatch和state, 以兼容函数形式的action
 * 使用redux-actions创建的action,会被包裹成为对象形式: { type: Type, payload: func }
 * 该中间件将函数执行后的结果作为payload传入下一个中间件
 */
function createThunkMiddleware() {
  return ({ dispatch, getState }) => next => action => {
    const { payload } = action;
    if (typeof payload === 'function') {
      action.payload = payload(dispatch, getState);
    }

    return next(action);
  };
}

const thunk = createThunkMiddleware();

export default thunk;
